<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
	<link type="text/css" href="css/main.css" rel="stylesheet" />
	<script type="text/javascript">
		function showResult()
		{
			
		}
	</script>
</head>
<body>
	<form enctype="multipart/form-data" id ="form" action="#" method="post">
		選擇要上傳的csv檔案<input name="csv" type="file" /><br>
		<input type="submit" value="上傳" /> 
	</form>		
	<?php		
		ini_set("display_errors","2");
		include "tools/tools.php";
		if(!empty($_FILES['csv']))
		{
			/**接收參數陣列
				檔案名稱
					$_FILES['userfile']['name']			
				檔案格式(image/jpeg)
					$_FILES['userfile']['type']
				檔案大小
					$_FILES['userfile']['size']
				檔案暫存的位置(伺服器上暫存的位置,必須要移動到正確的位置)
					$_FILES['userfile']['tmp_name']
				錯誤訊息
					$_FILES['userfile']['error']
			 * 
			 */
			include "Uploader.php";
			$file = $_FILES['csv'];//取得上傳檔案陣列			
			$fileName = $file['name'];
			$ext = "";			
			$size = $file['size'];
			$tmpName = $file['tmp_name'];
								
			/**
			 * @var Uploader 上傳用物件
			 */
			$uploader = new Uploader();			
			$uploader->setUploadFile($file,1,$fileName);//設定要上傳的檔案
			$uploader->setFileSize($size);//設定上傳檔大小
			$ext = $uploader->getFileExt($fileName);//取出副檔名
			$uploader->setMaxFileSize(10240);//設定10mb上傳大小
			$uploader->setTmpFile($tmpName);//設定暫存檔
			if($uploader->isValidExt($ext))//判斷是否為合法副檔名
			{
				echo "<div class='info'>請上傳附檔名為csv之檔案!!</div>";
			}									
			else
			{
				if($uploader->upload())
				{					
					Tools::processCSV($fileName);
					echo "<div class='info'>上傳成功!!"					
					."</div>";
					
				}
				else
				{
					echo "<div class='info'>上傳失敗，請聯絡#7031!!</div>";
				}
			}		
		}
		else
		{
			echo "尚未上傳csv..";
		}	
	?>
</body>
</html>

